﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using DevExpress.Data;
using DevExpress.Web;
using log4net;
using Microsoft.AspNet.Membership.OpenAuth;
using Timesheet.Service;

namespace Timesheet.WebForm.Account
{
    public partial class Manage : System.Web.UI.Page
    {

        private static readonly ILog logger = LogManager.GetLogger(typeof(Manage));

        public IUserService UserService { get; set; }

        protected void Page_Load()
        {
            if (!IsPostBack)
            {
             
            }
        }

        protected void odsUser_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = UserService; 
        }

        protected void gvUser_BeforePerformDataSelect(object sender, EventArgs e)
        {
            Session["IdUser"] = (sender as ASPxGridView).GetMasterRowKeyValue();
        }

        protected void gvUser_CustomColumnSort(object sender, CustomColumnSortEventArgs e)
        {
            if (e.Column != null && (e.Column.FieldName == "Manager" || e.Column.FieldName == "Coordinator"
                || e.Column.FieldName == "TeamLead" || e.Column.FieldName == "Active"))
            {

                    object city1 = e.Value1;

                    object city2 = e.Value2;

                    int res = Comparer.Default.Compare(city2 , city1);

                    if (res == 0)
                    {
                        object country1 = e.GetRow1Value("LastName");

                        object country2 = e.GetRow2Value("LastName");

                        if (e.Column.SortOrder == ColumnSortOrder.Descending)
                        {

                            res = Comparer.Default.Compare(country2, country1);
                        }
                        else
                        {
                            res = Comparer.Default.Compare(country1, country2);
                        }

                    }

                    e.Result = res;

                    e.Handled = true;
                

            }
        }

        protected void gvUser_CustomErrorText(object sender, ASPxGridViewCustomErrorTextEventArgs e)
        {
            if (e.Exception is NullReferenceException)
                e.ErrorText = "NullReferenceException Text";
            else if (e.Exception is InvalidOperationException)
                e.ErrorText = "InvalidOperationException Text";
            else if (e.Exception is Exception)
            {
                if (e.Exception.InnerException is CustomException)
                {
                    e.ErrorText = e.Exception.InnerException.Message;
                }
                else
                    e.ErrorText = e.Exception.Message;
            }
        }
    }
}